﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WinFormsAppTest.Dao;

namespace WinFormsAppTest
{
    public partial class UserUpdateForm : Form
    {
        public string username;
        public string password;
        public string adminname;
        public int adminid;
        public int userid;

        public UserUpdateForm()
        {
            username = "null";
            password = "null";
            InitializeComponent();
        }

        public UserUpdateForm(int uid,string uname, string upassword, string aname, int aid)
        {
            username = uname;
            password = upassword;
            adminname = aname;
            adminid = aid;
            userid = uid;
            InitializeComponent();
        }

        private void UserUpdate_Closing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.UserClosing)
            {
                // 关闭进程
                Process.GetCurrentProcess().Kill();
            }
        }

        private void UserUpdate_Load(object sender, EventArgs e)
        {
            UserNametextBox.Text = username;
            PasswordtextBox.Text = password;
        }

        private void Exitbutton_Click(object sender, EventArgs e)
        {
            this.Hide();
            UserForm f = new UserForm(adminname, adminid);
            f.ShowDialog();
            this.Dispose();
        }

        private void OKbutton_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(UserNametextBox.Text))
            {
                MessageBox.Show("添加账号不能为空！");
            }
            else if (string.IsNullOrWhiteSpace(PasswordtextBox.Text))
            {
                MessageBox.Show("添加密码不能为空！");
            }

            if (!string.IsNullOrWhiteSpace(UserNametextBox.Text)
                && !string.IsNullOrWhiteSpace(PasswordtextBox.Text))
            {
                //直接调用dao层进行添加
                AdminDao admindao = new AdminDao();
                admindao.UpdateUserById(userid, UserNametextBox.Text, PasswordtextBox.Text);
                MessageBox.Show("更新成功！");
                UserForm f2 = new UserForm(adminname, adminid);
                this.Hide();
                f2.ShowDialog();
                this.Dispose();

            }
        }
    }
}
